package com.ruoyi.project.common;

import com.alibaba.fastjson.JSON;
import com.baidu.ueditor.ActionEnter;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.UUID;

@Controller
public class UeditorController {
    @RequestMapping(value="/config")
    public void config(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json");
        String rootPath ="C:\\Program Files\\Apache Software Foundation\\apache-tomcat-9.0.16\\webapps\\static\\admin\\ueditor\\jsp";
        System.out.println(rootPath);
        try {
            response.setCharacterEncoding("UTF-8");
            String exec = new ActionEnter(request, rootPath).exec();
            System.out.println(exec);
            PrintWriter writer = response.getWriter();
            writer.write(new ActionEnter( request, rootPath ).exec());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
//    @RequestMapping(value = "/upload",method= RequestMethod.POST)
//	public String uploadImage(HttpServletRequest request, HttpServletResponse response)
//		throws Exception {
//
//		response.setContentType("text/html;charset=UTF-8");
//		MultipartHttpServletRequest multipartRequest =(MultipartHttpServletRequest) request;
//		MultipartFile file = multipartRequest.getFile("upfile");
//		String savePath="C:\\Users\\Messi\\Desktop\\img";
//		InputStream is=file.getInputStream();
//		String fileName = file.getOriginalFilename();
//		String id = UUID.randomUUID().toString();
//		String urlName = savePath+id+fileName.substring(fileName.lastIndexOf("."),fileName.length());
//		File tempFile =new File(urlName);
//		try{
//			file.transferTo(tempFile);
//			FileUtils.copyInputStreamToFile(is,tempFile);
//		}catch (Exception e){
//			e.printStackTrace();
//		} finally {
//			{
//				if(null != is){
//					try{
//						is.close();
//					}catch (IOException e){
//						e.printStackTrace();
//					}
//				}
//			}}
//
//			ReturnUploadImage rui = new ReturnUploadImage();
//			if(tempFile != null){
//				rui.setState("SUCCESS");
//				rui.setUrl(urlName);
//				rui.setTitle(fileName);
//				rui.setOriginal(fileName);
//			}
//			String result= JSON.toJSONString(rui);
//			response.getWriter().write(result);
//			return null;
//		
//    }
   
}
